// 数据库操作
const mongoose = require('mongoose');
// 对象规则验证
const Joi = require('joi');
// 模型规则类
const Schema = mongoose.Schema;
// 商品分类规则 一级分类
const categoryOneModel = new Schema({
	// 分类名称
		title: {
			type: String,
			minlength: 2,
			maxlength: 30,
			required: true
		},
		cate_level: {
			type: Number,
			default: 1
		},
		children: [{
			type: mongoose.Schema.Types.ObjectId,
			ref: 'CategoryTwo'
		}],
		// 是否有效 
		isValid: {
			type: Boolean,
			default: true
		},
		// 创建时间
		createTime: {
			type: Date,
			default: Date.now
		}
}, {versionKey: false});

// 商品分类规则 二级分类
const categoryTwoModel = new Schema({
	// 分类名称
		title: {
			type: String,
			minlength: 2,
			maxlength: 30,
			required: true,
		},
		cate_level: {
			type: Number,
			default: 2
		},
		children: [{
			type: mongoose.Schema.Types.ObjectId,
			ref: 'CategoryThree'
		}],
		// 是否有效 
		isValid: {
			type: Boolean,
			default: true
		},
		// 创建时间
		createTime: {
			type: Date,
			default: Date.now
		}
}, {versionKey: false});

// 商品分类规则 三级分类
const categoryThreeModel = new Schema({
	// 分类名称
		title: {
			type: String,
			minlength: 2,
			maxlength: 30,
			required: true,
		},
		cate_level: {
			type: Number,
			default: 3
		},
		// 分类图片
		icon: {
			type: String,
			default: ''
		},
		// 是否有效 
		isValid: {
			type: Boolean,
			default: true
		},
		// 创建时间
		createTime: {
			type: Date,
			default: Date.now
		}
}, {versionKey: false});

// 商品分类动态参数规则
const dynamicAttrModel = new Schema({
	// 参数名称
		attr_name: {
			type: String,
			minlength: 2,
			maxlength: 30,
			required: true,
		},
		attr_vals: [{
			type: String,
			default: ''
		}],
		cate_id: {
			type: mongoose.Schema.Types.ObjectId,
			ref: 'CategoryTwo'
		},
		// 创建时间
		add_time: {
			type: Date,
			default: Date.now
		}
}, {versionKey: false});

// 商品分类动态参数规则
const staticAttrModel = new Schema({
	// 参数名称
		attr_name: {
			type: String,
			minlength: 2,
			maxlength: 30,
			required: true,
		},
		attr_vals: [{
			type: String,
			default: ''
		}],
		cate_id: {
			type: mongoose.Schema.Types.ObjectId,
			ref: 'CategoryTwo'
		},
		// 创建时间
		add_time: {
			type: Date,
			default: Date.now
		}
}, {versionKey: false});

// 商品分类静态属性规则

// 创建一级分类集合 
const CategoryOne = mongoose.model('Category_one',categoryOneModel,'Category_one');
// 创建二级分类集合 
const CategoryTwo = mongoose.model('Category_two',categoryTwoModel,'Category_two');
// 创建三级分类集合 
const CategoryThree = mongoose.model('Category_three',categoryThreeModel,'Category_three');
// 创建动态参数集合 
const goodsDyAttr = mongoose.model('goods_dy_attr',dynamicAttrModel,'goods_dy_attr');
// 创建静态参数集合 
const goodsStaticAttr = mongoose.model('goods_static_attr',staticAttrModel,'goods_static_attr');


// 商品分类格式校验
const validateCategory = category => {
	// 定义对象验证规则
	const schema = {
		title: Joi.string().min(2).max(30).required().error(new Error('分类名称不符合验证验证规则')),
		createTime: Joi.date().default(Date.now, 'created time'),
	};
	// 验证
	return Joi.validate(category, schema, {
		// 检测到所有错误
		abortEarly: false,
		// 允许对象包含被忽略的未知键
		allowUnknown: true
	});
}

// 商品参数校验
const validateGoodsAttr = goodsAttr => {
	// 定义对象验证规则
	const schema = {
		attr_name: Joi.string().min(2).max(30).required().error(new Error('分类名称不符合验证验证规则')),
		add_time: Joi.date().default(Date.now, 'created time'),
	};
	// 验证
	return Joi.validate(goodsAttr, schema, {
		// 检测到所有错误
		abortEarly: false,
		// 允许对象包含被忽略的未知键
		allowUnknown: true
	});
}

// 导出模块成员
module.exports = {
	CategoryOne,
	CategoryTwo,
	CategoryThree,
	goodsDyAttr,
	goodsStaticAttr,
	validateGoodsAttr,
	validateCategory
}
